﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CongThongTin.Entities;
using System.Text;
using System.Data;

namespace CongThongTin
{
    public partial class CopyMyInfo : System.Web.UI.Page
    {
        CongThongTinEntities _entities;
        int _maLoaiTin;
        int _pageSite = 5;
        PagedDataSource pds;

        public static List<TINTUC> lTinTuc;
       

        public int CurrentPage
        {

            get
            {
                if (this.ViewState["CurrentPage"] == null)
                    return 0;
                else
                    return Convert.ToInt16(this.ViewState["CurrentPage"].ToString());
            }

            set
            {
                this.ViewState["CurrentPage"] = value;
            }

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            _maLoaiTin = int.Parse(Request.Params["tl"].ToString());
            _entities = new CongThongTinEntities();
            loadTitle();
            loadTinTucTop();
            loadAllTinTuc();
        }
        void loadTitle()
        {
            LOAITIN oLoaiTin = _entities.LOAITINs.FirstOrDefault(lt => lt.MALOAITIN == _maLoaiTin);
            tbTitle.Text="<h2 class='title'>"+oLoaiTin.TENLOAITIN+"</h2>";
        }
        void loadAllTinTuc()
        {
            lTinTuc = new List<TINTUC>();
           
            Entities.LOAITIN oGetLoaiTin = _entities.LOAITINs.SingleOrDefault(l => l.MALOAITIN == _maLoaiTin);
            int maLoai = Convert.ToInt32(oGetLoaiTin.MALOAITIN);

            lTinTuc = (from t in _entities.TINTUCs
                           where t.MALOAITINTUC == maLoai
                           select t).ToList();

            if (lTinTuc.Count > 0)
                initPagingControl(true);
            else
                initPagingControl(false);

            pds = new PagedDataSource();
            pds.DataSource = lTinTuc;
            pds.AllowPaging = true;
            pds.PageSize = _pageSite;
            pds.CurrentPageIndex = CurrentPage;
            lnkbtnNext.Enabled = !pds.IsLastPage;
            lnkbtnPrevious.Enabled = !pds.IsFirstPage;        

            StringBuilder html = new StringBuilder();
            int count = 0;
            foreach (TINTUC item in pds)
            {
                string Imageurl = "ImageHandler.ashx?option=2&id=" + item.MATINTUC;
                string tintucUrl = "ChiTiet.aspx?id=" + item.MATINTUC;
                if (count == 0)
                    html.Append(" <ol id=\"singlecomments\" class=\"commentlist\">");

                html.Append("<li class= \"clearfix\">");
                html.Append("<div class=\"user\"><img alt=\"\" src=\"" + Imageurl + "\" class=\"avatar\" /></div>");
                html.Append("<div class=\"message\"> <div class=\"info\">");
                html.Append("<h3><a href=\"" + tintucUrl + "\">" + item.TIEUDE + "</a></h3> </div>");
                html.Append("<br/><br/><p>" + item.TOMTAT + "...</p>");
                html.Append("</div><div class=\"clear\"></div></li> ");

                if (count == lTinTuc.Count())
                    html.Append("</ol>");

                count++;
            }
            string script = @"  $(function () { $('#comments').html(''); $('#comments').append('" + html.ToString() + "'); });";
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString() + "RegisterLoadAllTinTuc", script, true);

            doPaging();
        }
        void loadTinTucTop()
        {
            Entities.LOAITIN oGetLoaiTin = _entities.LOAITINs.SingleOrDefault(tl => tl.MALOAITIN==_maLoaiTin);
            int maLT = Convert.ToInt32(oGetLoaiTin.MALOAITIN);

            var vTinTuc = _entities.TINTUCs.Where(t => t.MALOAITINTUC == maLT).Take(1);
            Entities.TINTUC oTopTinTuc = new TINTUC();

            foreach (var item in vTinTuc)
            {
                oTopTinTuc.MATHELOAI = item.MATHELOAI;
                oTopTinTuc.LOAITIN = item.LOAITIN;
                oTopTinTuc.MATINTUC = item.MATINTUC;
                oTopTinTuc.TIEUDE = item.TIEUDE;
                oTopTinTuc.TOMTAT = item.TOMTAT;
                oTopTinTuc.HINHMOTA = item.HINHMOTA;
            }

            if (vTinTuc != null)
            {
                string myHTMLTinTucTop = @"
                <img src='ImageHandler.ashx?option=2&id=" + oTopTinTuc.MATINTUC + "' alt='' width='250px' height='150px' /><div class='info'><h4><a href='ChiTiet.aspx?id=" + oTopTinTuc.MATINTUC + "'>" + oTopTinTuc.TIEUDE + "</a></h4></div><p>" + oTopTinTuc.TOMTAT + "</p>";
                tbTopContent.Text = myHTMLTinTucTop;
            }
        }



        void initPagingControl(bool status)
        {
            lnkbtnNext.Visible = status;
            lnkbtnPrevious.Visible = status;
            dlPaging.Visible = status;
        }
        private void doPaging()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("PageIndex");
            dt.Columns.Add("PageText");
            for (int i = 0; i < pds.PageCount; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = i;
                dr[1] = i + 1;
                dt.Rows.Add(dr);
            }

            dlPaging.DataSource = dt;
            dlPaging.DataBind();
        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName.Equals("lnkbtnPaging"))
            {

                CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
                loadAllTinTuc();
            }
        }

        protected void lnkbtnPrevious_Click(object sender, EventArgs e)
        {
            CurrentPage -= 1;
            loadAllTinTuc();
        }

        protected void lnkbtnNext_Click(object sender, EventArgs e)
        {
            CurrentPage += 1;
            loadAllTinTuc();
        }

        //protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    CurrentPage = 0;
        //    loadAllTinTuc();
        //}

        protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
            if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
            {
                lnkbtnPage.Enabled = false;
                lnkbtnPage.Font.Bold = true;
            }
        }
    }
}